package com.example.myapplication.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.example.myapplication.util.information;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class f7 extends Fragment {
    View view;
    Context context;
    private ListView f7Listview;
    information information;
    List<information>list=new ArrayList<>();
    F7ItemAdapter adapter;
    public f7() {
        // Required empty public constructor
    }
    private String places[]={"武汉","湖北","山东","北京","南京","天津","南昌",
            "上海","广州","惠州","九江","辽宁","曲阳","山西","北京",};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_f7, container, false);
        context = getContext();
        f7Listview = (ListView) view.findViewById(R.id.f7_listview);
        for (int i = 0; i <15 ; i++) {
            information=new information("编号"+(1+i),places[i]);
            information.setImage(getResources().getIdentifier(context.getPackageName()+":drawable/image"+(i+1),null,null));
            list.add(information);
            }
        adapter=new F7ItemAdapter(context,list);
        f7Listview.setAdapter(adapter);
        f7Listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                information in=list.get(position);
                Toast.makeText(context, "我是"+in.getName()+",我在"+String.valueOf(in.getContent()), Toast.LENGTH_SHORT).show();
            }
        });
        return view;

    }
/*
适配器
 */
    class F7ItemAdapter extends BaseAdapter {

        private List<information> objects = new ArrayList<>();

        private Context context;
        private LayoutInflater layoutInflater;

        public F7ItemAdapter(Context context,List<information> objects) {
            this.context = context;
            this.layoutInflater = LayoutInflater.from(context);
            this.objects=objects;
        }

        @Override
        public int getCount() {
            return objects.size();
        }

        @Override
        public Object getItem(int position) {
            return objects.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.f7_item, null);
                holder=new ViewHolder();
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.text1 = (TextView) convertView.findViewById(R.id.text1);
                holder.text2 = (TextView) convertView.findViewById(R.id.text2);
                convertView.setTag(holder);
            }else {
                holder=(ViewHolder)convertView.getTag();
            }
            information information=objects.get(position);
            holder.image.setImageResource(information.getImage());
            holder.text1.setText(information.getName());
            holder.text2.setText(information.getContent());
            return convertView;
        }


        protected class ViewHolder {
            private ImageView image;
            private TextView text1;
            private TextView text2;
        }
    }
}
